<?php require_once('../Connections/learnConn.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "admin";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && false) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "../login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE jadwal SET kd_kelas=%s, kd_kat_kelas=%s, tahun_ajaran=%s, kd_matpel=%s, nik=%s, hari=%s, jam=%s WHERE kd_jadwal=%s",
                       GetSQLValueString($_POST['kd_kelas'], "text"),
                       GetSQLValueString($_POST['kd_kat_kelas'], "text"),
                       GetSQLValueString($_POST['tahun_ajaran'], "text"),
                       GetSQLValueString($_POST['kd_matpel'], "text"),
                       GetSQLValueString($_POST['nik'], "text"),
                       GetSQLValueString($_POST['hari'], "text"),
                       GetSQLValueString($_POST['jam'], "text"),
                       GetSQLValueString($_POST['kd_jadwal'], "int"));

  mysql_select_db($database_learnConn, $learnConn);
  $Result1 = mysql_query($updateSQL, $learnConn) or die(mysql_error());

  $updateGoTo = "jadwal.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

mysql_select_db($database_learnConn, $learnConn);
$query_guru = "SELECT nik, nama FROM pegawai";
$guru = mysql_query($query_guru, $learnConn) or die(mysql_error());
$row_guru = mysql_fetch_assoc($guru);
$totalRows_guru = mysql_num_rows($guru);

$colname_jadwal = "-1";
if (isset($_GET['kd_jadwal'])) {
  $colname_jadwal = $_GET['kd_jadwal'];
}
mysql_select_db($database_learnConn, $learnConn);
$query_jadwal = sprintf("SELECT * FROM jadwal WHERE kd_jadwal = %s", GetSQLValueString($colname_jadwal, "int"));
$jadwal = mysql_query($query_jadwal, $learnConn) or die(mysql_error());
$row_jadwal = mysql_fetch_assoc($jadwal);
$totalRows_jadwal = mysql_num_rows($jadwal);

$colname_matpel = $row_jadwal['kd_kelas'];
mysql_select_db($database_learnConn, $learnConn);
$query_matpel = sprintf("SELECT * FROM matpel WHERE nama_matpel LIKE %s", GetSQLValueString("%" . $colname_matpel . "%", "text"));
$matpel = mysql_query($query_matpel, $learnConn) or die(mysql_error());
$row_matpel = mysql_fetch_assoc($matpel);
$totalRows_matpel = mysql_num_rows($matpel);

mysql_select_db($database_learnConn, $learnConn);
$query_jam = "SELECT * FROM jam";
$jam = mysql_query($query_jam, $learnConn) or die(mysql_error());
$row_jam = mysql_fetch_assoc($jam);
$totalRows_jam = mysql_num_rows($jam);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Jadwal</title>
<link href="../style/style.css" type="text/css" rel="stylesheet" />
<link href="../style/pro_drop_1.css" type="text/css" rel="stylesheet"/>
<script src="../style/stuHover.js" type="text/javascript"></script>
</head>

<body><center>
<table width="900" bgcolor="#FFFFFF">
	<?php
	include "topmenu_admin.php";
	?>
    <tr>
    	<td id="menu">Admin Menu :
      <ul>
        	<li><a href="index.php">Home</a></li>
            <li><a href="siswa.php">Siswa</a></li>
            <li><a href="pegawai.php">Pegawai</a></li>
            <li><a href="matpel.php">Mata Pelajaran</a></li>
            <li><a href="jadwal.php">Jadwal</a></li>
            <li><a href="pengumuman.php">Pengumuman</a></li>
            <li><a href="buku_tamu.php">Buku Tamu</a></li>
            <li><a href="../logout.php">Logout</a></li>
        </ul>
        </td>
    	<td id="konten"><p>&nbsp;</p>
          <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
            <table align="center">
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Kelas:</td>
                <td><select name="kd_kelas">
                  <option value="7" <?php if (!(strcmp(7, htmlentities($row_jadwal['kd_kelas'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>7</option>
                  <option value="8" <?php if (!(strcmp(8, htmlentities($row_jadwal['kd_kelas'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>8</option>
                  <option value="9" <?php if (!(strcmp(9, htmlentities($row_jadwal['kd_kelas'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>9</option>
                </select>
                  <select name="kd_kat_kelas">
                    <option value="A" <?php if (!(strcmp("A", htmlentities($row_jadwal['kd_kat_kelas'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>A</option>
                    <option value="B" <?php if (!(strcmp("B", htmlentities($row_jadwal['kd_kat_kelas'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>B</option>
                </select></td>
                </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Tahun Ajaran:</td>
                <td><?php $date = getdate();
				$tahun = $date['year'];
				$tahun2 = $date['year']+1;
				$gabung = $tahun."/".$tahun2;
				?><select name="tahun_ajaran">
                  <option value="<?php echo $gabung; ?>"><?php echo $gabung; ?></option>
                  
                </select></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Mata Pelajaran:</td>
                <td><select name="kd_matpel">
                  <?php 
do {  
?>
                  <option value="<?php echo $row_matpel['kd_matpel']?>" <?php if (!(strcmp($row_matpel['kd_matpel'], htmlentities($row_jadwal['kd_matpel'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>><?php echo $row_matpel['nama_matpel']?></option>
                  <?php
} while ($row_matpel = mysql_fetch_assoc($matpel));
?>
                </select></td>
              </tr>
              <tr> </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Guru Pengajar:</td>
                <td><select name="nik">
                  <?php 
do {  
?>
                  <option value="<?php echo $row_guru['nik']?>" <?php if (!(strcmp($row_guru['nik'], htmlentities($row_jadwal['nik'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>><?php echo $row_guru['nama']?></option>
                  <?php
} while ($row_guru = mysql_fetch_assoc($guru));
?>
                </select></td>
              </tr>
              <tr> </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Hari:</td>
                <td><select name="hari">
                  <option value="Senin" <?php if (!(strcmp("Senin", htmlentities($row_jadwal['hari'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>Senin</option>
                  <option value="Selasa" <?php if (!(strcmp("Selasa", htmlentities($row_jadwal['hari'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>Selasa</option>
                  <option value="Rabu" <?php if (!(strcmp("Rabu", htmlentities($row_jadwal['hari'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>Rabu</option>
                  <option value="Kamis" <?php if (!(strcmp("Kamis", htmlentities($row_jadwal['hari'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>Kamis</option>
                  <option value="Jumat" <?php if (!(strcmp("Jumat", htmlentities($row_jadwal['hari'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>Jumat</option>
                </select></td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">Jam:</td>
                <td>
                  <select name="jam">
                     <?php do { ?> <option value="<?php echo $row_jam['jam']; ?>" <?php if (!(strcmp($row_jam['jam'], htmlentities($row_jadwal['jam'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>><?php echo $row_jam['jam']; ?></option>
                  <?php } while ($row_jam = mysql_fetch_assoc($jam)); ?></select>
                    </td>
              </tr>
              <tr valign="baseline">
                <td nowrap="nowrap" align="right">&nbsp;</td>
                <td><input type="submit" value="Edit Jadwal" /></td>
              </tr>
            </table>
            <input type="hidden" name="kd_jadwal" value="<?php echo $row_jadwal['kd_jadwal']; ?>" />
            <input type="hidden" name="MM_update" value="form1" />
            <input type="hidden" name="kd_jadwal" value="<?php echo $row_jadwal['kd_jadwal']; ?>" />
          </form>
        <p>&nbsp;</p></td>
    </tr>
    <tr>
    	<td colspan="2" id="footer">Copyright 2012</td>
    </tr>
    
</table></center>
</body>
</html>
<?php
mysql_free_result($matpel);

mysql_free_result($jam);

mysql_free_result($guru);

mysql_free_result($jadwal);
?>
